package com.neu.controller;

import com.neu.constant.ResultCodeEnum;
import com.neu.pojo.Plan;
import com.neu.pojo.Project;
import com.neu.service.PlanService;
import com.neu.service.ProjectService;
import com.neu.utils.Result;
import com.neu.vo.PlanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping(value = "/project", produces = {"application/json;charset=UTF-8"})
public class ProjectController {

    @Resource
    private ProjectService projectService;

    @Resource
    private PlanService planService;

    @GetMapping("/list")
    public Object list(@RequestParam("currentPage") int currentPage,
                       @RequestParam("pageSize") int pageSize) {
        log.info("查询项目信息: currentPage:{},pageSize:{}", currentPage,pageSize);
        return projectService.list(currentPage,pageSize);
//        log.info("查询项目信息:{}", id);
//        if (id <= 0) {
//            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
//        }
//        return projectService.info(id);
    }

    @GetMapping("/r")
    public Object read(@RequestParam("id") int id) {
        log.info("查询项目信息:{}", id);
        if (id <= 0) {
            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
        }
        return projectService.info(id);
    }

    @PostMapping("/w")
    public Object update(@RequestBody Project project) {
        log.info("修改项目信息:{}", project);
        return projectService.updateProject(project);
    }

    @PostMapping("/i")
    public Object insert(@RequestBody Project project) {
        log.info("新增项目:{}", project);
        return projectService.insertProject(project);
    }

    @PostMapping("/d")
    public Object delete(@RequestBody Project project) {
        log.info("删除项目:{}", project);
        return projectService.deleteProject(project);
    }

    @PostMapping("/bi")
    public Object batchInsertProject(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("导入项目");
        return projectService.batchInsertProject(file);
    }

    @GetMapping("/plan/list")
    public Object listPlan(@RequestParam("projectId") int projectId) {
        log.info("查询项目工作计划:{}",projectId);
        if (projectId <= 0) {
            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
        }
        return planService.list(projectId);
    }

    @PostMapping("/plan/w")
    public Object updatePlan(@RequestBody Plan plan) {
        log.info("修改项目工作计划信息:{}", plan);
        return planService.update(plan);
    }

    @PostMapping("/plan/i")
    public Object insertPlan(@RequestBody Plan plan) {
        log.info("新增项目工作计划:{}", plan);
        return planService.add(plan);
    }

    @PostMapping("/plan/d")
    public Object deletePlan(@RequestBody Plan plan) {
        log.info("删除项目工作计划:{}", plan);
        return planService.delete(plan);
    }

    @PostMapping("/plan/bi")
    public Object batchInsertPlan(@RequestParam("file") MultipartFile file,
                                  @RequestParam("projectId") int projectId) throws IOException {
        log.info("导入项目工作计划:{}", projectId);
        if (projectId <= 0) {
            return Result.error(ResultCodeEnum.PARAM_CHECK_ERROR);
        }
        return planService.batchInsertPlan(file,projectId);
    }

//    @PostMapping("/plan/ilist")
//    public Object add(@RequestBody PlanVO planVO) {
//        log.info("新增项目工作计划:{}",planVO);
//        return planService.add(planVO);
//    }

}
